package com.liuf.thread.runable;

import com.liuf.thread.TaskDemo.Task;

import java.util.concurrent.*;

/**
 * @author LIUFANG
 */
public class ExecutorDemo {

    private ExecutorService executorService;

    public ExecutorDemo(){
        /**初始化线程池*/
        this.executorService = newFixedThreadPool(5, 10);
    }

    /**初始化线程数,这种方式已经不推荐了*/
    private static Executor executor = Executors.newFixedThreadPool(6);

    public static void main(String[] args){
        ExecutorService executorService = ExecutorDemo.newFixedThreadPool(5,10);
        for (int i=0;i<=50;i++){
//            executor.execute(new Task());
            executorService.execute(new Task());
        }
    }

    /**
     *  创建一个线程池
     * @param corePoolSize 线程池中的核心线程数
     * @param maximumPoolSize 线程池中允许的最大线程数
     * @return  线程池连接
     */
    public static ExecutorService newFixedThreadPool(int corePoolSize,int maximumPoolSize){
        return new ThreadPoolExecutor(corePoolSize,maximumPoolSize,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
    }

}
